﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class MyAssignments : System.Web.UI.Page
{
    DataTable dt;
    //static object userId;
    public string status;
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!IsPostBack)
        {
            //userId = Session["UserId"];
            countNoOfAssignments();
            UserSelection.sObj.Clear();
            LoadSubject();
            LoadTopics();
            Session["Status"] = "Scheduled";
            bindGrid("Scheduled");
        }
        status = Session["Status"].ToString();
    }

    private void LoadSubject()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetSubjects();
        ListItem list = new ListItem();
        list.Text = "All";
        list.Value = "0";
        ddlSub.Items.Add(list);
        ddlSub.SelectedIndex = ddlSub.Items.IndexOf(ddlSub.Items.FindByText("All"));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            ddlSub.Items.Add(li);
        }
    }

    private void LoadTopics()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.getAllTopics();
        ListItem list = new ListItem();
        list.Text = "All";
        list.Value = "0";
        ddlTopic.Items.Add(list);
        ddlTopic.SelectedIndex = ddlTopic.Items.IndexOf(ddlTopic.Items.FindByText("All"));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][1].ToString();
            li.Value = dt.Rows[i][0].ToString();
            ddlTopic.Items.Add(li);
        }

    }

    protected void ClearRecord_Click(object sender, EventArgs e)
    {
        gvAssignment.DataSource = null;
        gvAssignment.DataBind();
    }

    private void bindGrid(string status)
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetAssignmentInfo(Convert.ToInt32(ddlSub.SelectedValue), Convert.ToInt32(ddlTopic.SelectedValue),status, Session["UserId"]);
        gvAssignment.DataSource = dt;
        gvAssignment.DataBind();
    }

    private void countNoOfAssignments()
    {
        DataSet dsCntAss = new DataSet();
        dsCntAss = AdminCreateTestDAL.GetCountOfAssignment(Session["UserId"]);
        lablScheduledCount.Text = dsCntAss.Tables[0].Rows[0][0].ToString();
        lablPendingCount.Text = dsCntAss.Tables[1].Rows[0][0].ToString();
        lablCompletedCount.Text = dsCntAss.Tables[2].Rows[0][0].ToString();
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {
        bindGrid(status);
    }

    protected void lnkTestScheduled_Click(object sender, EventArgs e)
    {
        gvAssignment.DataSource = null;
        gvAssignment.DataBind();
        Session["Status"]="Scheduled";
        status = Session["Status"].ToString();
        gvAssignment.Columns[7].Visible = true;
    }

    protected void lnkTestPending_Click(object sender, EventArgs e)
    {
        gvAssignment.DataSource = null;
        gvAssignment.DataBind();
        Session["Status"] = "Pending";
        status = Session["Status"].ToString();
        gvAssignment.Columns[7].Visible = true;
    }

    protected void lnkTestCompteted_Click(object sender, EventArgs e)
    {
        gvAssignment.DataSource = null;
        gvAssignment.DataBind();
        Session["Status"] = "Completed";
        status = Session["Status"].ToString();
        gvAssignment.Columns[7].Visible = false;
    }

    protected void gvAssignment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvAssignment.PageIndex = e.NewPageIndex;
        gvAssignment.SelectedIndex = -1;
        bindGrid(status);
    }

    protected void ddlSub_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlTopic.Items.Clear();
        if (ddlSub.SelectedItem.Text == "--Select One--")
        {
            lablError.Text = "Please select one subject";

        }
        else
        {
            dt = new DataTable();
            ListItem list = new ListItem();
            list.Text = "All";
            list.Value = "0";
            ddlTopic.Items.Add(list);
            dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dt.Rows[i][0].ToString();
                li.Value = dt.Rows[i][1].ToString();
                ddlTopic.Items.Add(li);
            }
        }
    }

    protected void gvAssignment_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int assId = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "btnLinkClick": Response.Redirect("~/Student/ViewStudentAssignmentByStudent.aspx?AssignmentId=" + assId.ToString());
                break;

            case "btnLinkRankClick":
                Response.Redirect("~/Student/ViewAssignmentRank.aspx?AssignmentId=" + assId.ToString());
                break;
        }

        
    }
}